package com.sjtucook;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.*;

public class FirstMenuActivity extends Activity {
	private Button search;
	private Button favorite;
	private Button toprated;
	private Button history;
	private Button update;
	private LinearLayout content;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.content);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.titlebar);
		content = (LinearLayout) findViewById(R.id.center_pane);
		// search button
		this.search = (Button) findViewById(R.id.tittle_bt);
		this.search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onSearchRequested();
			}
		});
		// favorite button
		this.favorite = (Button) findViewById(R.id.bottom_button_1);
		this.favorite.setText(R.string.FAVORITE);
		this.favorite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(FirstMenuActivity.this,DishListActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(DishListActivity.FLAG, DishListActivity.FAVORITE);
				intent.putExtras(bundle);
				startActivity(intent);
			}

		});
		// toprated button
		this.toprated = (Button) findViewById(R.id.bottom_button_2);
		this.toprated.setText(R.string.TOP_RATED);
		this.toprated.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(FirstMenuActivity.this,DishListActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(DishListActivity.FLAG, DishListActivity.RATE);
				intent.putExtras(bundle);
				startActivity(intent);
			}

		});
		// history button
		this.history = (Button) findViewById(R.id.bottom_button_3);
		this.history.setText(R.string.HISTORY);
		this.history.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(FirstMenuActivity.this,DishListActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(DishListActivity.FLAG, DishListActivity.HISTORY);
				intent.putExtras(bundle);
				startActivity(intent);

			}

		});
		// update button
		this.update = (Button) findViewById(R.id.bottom_button_4);
		this.update.setText(R.string.QUIT);
		this.update.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				exitDialog();

			}

		});

		// get CookApplication object
		CookApplication currentApp = (CookApplication) this
				.getApplicationContext();
		// db query
		ArrayList<HashMap<String, Object>> sourceList = currentApp
				.getDbadapter().queryFirstSort();
		if (sourceList.size() > 0) {
			// fill the contnet with a gridview
			GridView gridView = new GridView(this);
			gridView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			gridView.setNumColumns(GridView.AUTO_FIT);
			gridView.setScrollBarStyle(GridView.SCROLLBARS_OUTSIDE_OVERLAY);
			gridView.setSelector(R.color.white);
			content.addView(gridView);
			// 绑定数据源
			gridView.setAdapter(new SimpleAdapter(this, sourceList,
					R.layout.griditem, new String[] { DBAdapter.KEY_IMAGE,
							DBAdapter.KEY_NAME }, new int[] { R.id.ItemImage,
							R.id.ItemText }));
			// 绑定事件触发
			gridView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					HashMap<String, Object> hashMap = (HashMap<String, Object>) parent
							.getItemAtPosition(position);
					Intent intent = new Intent(FirstMenuActivity.this,
							SecondMenuActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString(DBAdapter.KEY_TABLE_NAME,
							(String) hashMap.get(DBAdapter.KEY_TABLE_NAME));
					bundle.putString(DBAdapter.KEY_COLUMN_NAME,
							(String) hashMap.get(DBAdapter.KEY_COLUMN_NAME));
					intent.putExtras(bundle);
					startActivity(intent);
				}
			});
		} else {
			//no sort category
			TextView textView = new TextView(this);
			textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			textView.setGravity(Gravity.CENTER);
			textView.setText("No Item Found");
			content.addView(textView);
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitDialog();
			return false;
		}
		return false;
	}

	protected void exitDialog() {
		android.content.DialogInterface.OnClickListener onclick = new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case android.app.Dialog.BUTTON_NEGATIVE:
					dialog.dismiss();
					break;
				case android.app.Dialog.BUTTON_POSITIVE:
					android.os.Process.killProcess(android.os.Process.myPid());
					break;
				}
			}
		};
		AlertDialog.Builder builder = new Builder(FirstMenuActivity.this);
		builder.setMessage("确定要退出吗?");
		builder.setTitle("提示");
		builder.setPositiveButton("确认", onclick);
		builder.setNegativeButton("取消", onclick);
		builder.create().show();
	}
}
/*
 * class drawCanvas extends View {
 * 
 * public drawCanvas(Context context) { super(context); }
 * 
 * @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);
 * 
 * // 建立 Paint 物件 Paint vPaint = new Paint(); Paint vPaint2 = new Paint();
 * 
 * // -------------------------------------------- // 设定颜色
 * vPaint.setColor(0xFFFFFF00);
 * 
 * // 实心矩形 canvas.drawRect(30, 50, 130, 150, vPaint);
 * 
 * // 设定阴影 (柔边, X轴位移, Y轴位移, 阴影颜色) vPaint.setShadowLayer(5, 3, 3, 0xFFFF00FF);
 * 
 * // 实心矩形 & 其阴影 canvas.drawRect(30, 200, 130, 300, vPaint);
 * 
 * // -------------------------------------------- // 设定颜色
 * vPaint2.setColor(0xFFFFFF00);
 * 
 * // 空心 vPaint2.setStyle(Style.STROKE);
 * 
 * // 空心矩形 canvas.drawRect(200, 50, 300, 150, vPaint2);
 * 
 * // 设定阴影 (柔边, X轴位移, Y轴位移, 阴影颜色) vPaint2.setShadowLayer(5, 3, 3, 0xFFFF00FF);
 * 
 * // 空心矩形 & 其阴影 canvas.drawRect(200, 200, 300, 300, vPaint2); } }
 */